﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_CmdQuit : System.Web.UI.UserControl
    {
        private IPositionTypeBLL ptBLL = new PositionTypeBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IClassBLL cBLL = new ClassBLL();
        private IJobSiteBLL jsBLL = new JobSiteBLL();
        private IMovmentBLL movBLL = new MovmentBLL();
        private ICommandDetailBLL dtlsBLL = new CommandDetailBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private SalaryGroupBLL salBLL = new SalaryGroupBLL();
        private EmployeeRequestBLL empreqBLL = new EmployeeRequestBLL();
        private SsMng ssMng = new SsMng();
        private const int ItemsPerRequest = 10;
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            txtOldSalary.Attributes.Add("onkeypress", "return isNumberKey(event)");

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                bindMovmentCombo();               

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {                    
                    lblSequence.Text = DataBinder.Eval(DataItem, "cmd_dtls_seq").ToString();

                    //// ชื่อ  สกุล
                    cmbEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "FULL_NAME").ToString(), DataBinder.Eval(DataItem, "EMP_SEQ").ToString()));
                    cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "EMP_SEQ").ToString();                    

                    // การเคลื่อนไหว
                    movmentCombo.SelectedValue = DataBinder.Eval(DataItem, "MOVE_SEQ").ToString();
                    //ตำแหน่งและส่วนราชการเดิม
                    cmbOldPosition.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "OLD_POS_TEXT").ToString(), DataBinder.Eval(DataItem, "OLD_POS_SEQ").ToString()));
                    cmbOldPosition.SelectedValue = DataBinder.Eval(DataItem, "OLD_POS_SEQ").ToString();
                    
                    cmbOldClass.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "OLD_CLASS_NAME").ToString(), DataBinder.Eval(DataItem, "OLD_CLASS_SEQ").ToString()));
                    cmbOldClass.SelectedValue = DataBinder.Eval(DataItem, "OLD_CLASS_SEQ").ToString();
                    txtOldSalary.Text = DataBinder.Eval(DataItem, "OLD_SALARY", "{0:N2}").ToString();

                    cmbOldJobSite.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "OLD_JOB_SITE_NAME").ToString(), DataBinder.Eval(DataItem, "OLD_JOB_SITE_SEQ").ToString()));
                    cmbOldJobSite.SelectedValue = DataBinder.Eval(DataItem, "OLD_JOB_SITE_SEQ").ToString();

                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();
                    txtStartDate.SetValue(DataBinder.Eval(DataItem, "START_DATE", "{0:dd/MM/yyyy}").ToString());
                }
            }
        }        

        private void bindMovmentCombo()
        {
            IList<MovmentInfo> infoList = movBLL.ListMovments(Session["EMPTYPE"].ToString());

            movmentCombo.DataSource = infoList;
            movmentCombo.DataBind();
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            string strPosition = ((RadComboBox)cmbOldPosition).Text;
            string[] str = strPosition.Split('-');

            dtlsBLL.Insert(int.Parse(Session["CMD"].ToString())
                , (int?)null
                , string.IsNullOrEmpty(txtStartDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtStartDate.GetStringValue())
                , (int?)null //string.IsNullOrEmpty(cmbNewPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbNewPosition.SelectedValue)
                , "" //str[0]
                , "" //str[1]//cmbNewPosition.SelectedItem.Text// txtPosition.Text.Trim()
                , (int?)null //string.IsNullOrEmpty(cmbNewClass.SelectedValue) ? (int?)null : int.Parse(cmbNewClass.SelectedValue)
                , (int?)null //string.IsNullOrEmpty(cmbNewJobSite.SelectedValue) ? (int?)null : int.Parse(cmbNewJobSite.SelectedValue)
                , "" //txtNewJobSiteName.Text.Trim()
                , (double?)null //string.IsNullOrEmpty(txtSalary.Text) ? (double?)null : double.Parse(txtSalary.Text)
                , string.IsNullOrEmpty(txtOldSalary.Text) ? (double?)null : double.Parse(txtOldSalary.Text)
                , string.IsNullOrEmpty(cmbOldPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbOldPosition.SelectedValue)
                , str[0]
                , str[1]
                , string.IsNullOrEmpty(cmbOldClass.SelectedValue) ? (int?)null : Int32.Parse(cmbOldClass.SelectedValue)
                , (int?)null
                , ((RadComboBox)cmbOldJobSite).Text
                , (int?)null //string.IsNullOrEmpty(txtEntNo.Text) ? (int?)null : Int32.Parse(txtEntNo.Text)
                , "" //((RadComboBox)cmbEntPosition).Text
                , (int?)null //string.IsNullOrEmpty(entPostTypeCombo.SelectedValue) ? (int?)null : Int32.Parse(entPostTypeCombo.SelectedValue)
                , (int?)null //string.IsNullOrEmpty(cmbEntClass.SelectedValue) ? (int?)null : Int32.Parse(cmbEntClass.SelectedValue)
                , "" //txtEntJobSiteName.Text
                , (DateTime?)null //string.IsNullOrEmpty(txtEntDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtEntDate.GetStringValue())
                , (int?)null //string.IsNullOrEmpty(cmbTmpEmployee.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEmployee.SelectedValue)
                , (int?)null //string.IsNullOrEmpty(cmbTmpEducation.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEducation.SelectedValue)
                , (int?)null
                , string.IsNullOrEmpty(movmentCombo.SelectedValue) ? (int?)null : Int32.Parse(movmentCombo.SelectedValue)
                , (DateTime?)null //string.IsNullOrEmpty(txtPosClassDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtPosClassDate.GetStringValue())
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strPosition = ((RadComboBox)cmbOldPosition).Text;
            string[] str = strPosition.Split('-');

            dtlsBLL.Update(int.Parse(lblSequence.Text)
                , (int?)null
                , string.IsNullOrEmpty(txtStartDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtStartDate.GetStringValue())
                 , (int?)null //string.IsNullOrEmpty(cmbNewPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbNewPosition.SelectedValue)
                , "" //str[0]
                , "" //str[1]//cmbNewPosition.SelectedItem.Text// txtPosition.Text.Trim()
                , (int?)null //string.IsNullOrEmpty(cmbNewClass.SelectedValue) ? (int?)null : int.Parse(cmbNewClass.SelectedValue)
                , (int?)null //string.IsNullOrEmpty(cmbNewJobSite.SelectedValue) ? (int?)null : int.Parse(cmbNewJobSite.SelectedValue)
                , "" //txtNewJobSiteName.Text.Trim()
                , (double?)null //string.IsNullOrEmpty(txtSalary.Text) ? (double?)null : double.Parse(txtSalary.Text)
                , string.IsNullOrEmpty(txtOldSalary.Text) ? (double?)null : double.Parse(txtOldSalary.Text)
                , string.IsNullOrEmpty(cmbOldPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbOldPosition.SelectedValue)
                , str[0]
                , str[1]
                , string.IsNullOrEmpty(cmbOldClass.SelectedValue) ? (int?)null : Int32.Parse(cmbOldClass.SelectedValue)
                , (int?)null
                , ((RadComboBox)cmbOldJobSite).Text
                , (int?)null //string.IsNullOrEmpty(txtEntNo.Text) ? (int?)null : Int32.Parse(txtEntNo.Text)
                , "" //((RadComboBox)cmbEntPosition).Text
                , (int?)null //string.IsNullOrEmpty(entPostTypeCombo.SelectedValue) ? (int?)null : Int32.Parse(entPostTypeCombo.SelectedValue)
                , (int?)null //string.IsNullOrEmpty(cmbEntClass.SelectedValue) ? (int?)null : Int32.Parse(cmbEntClass.SelectedValue)
                , "" //txtEntJobSiteName.Text
                , (DateTime?)null //string.IsNullOrEmpty(txtEntDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtEntDate.GetStringValue())
                , (int?)null //string.IsNullOrEmpty(cmbTmpEmployee.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEmployee.SelectedValue)
                , (int?)null //string.IsNullOrEmpty(cmbTmpEducation.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEducation.SelectedValue)
                , (int?)null
                , string.IsNullOrEmpty(movmentCombo.SelectedValue) ? (int?)null : Int32.Parse(movmentCombo.SelectedValue)
                , (DateTime?)null //string.IsNullOrEmpty(txtPosClassDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtPosClassDate.GetStringValue())
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        
        protected void cmbOldPosition_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbOldPosition.Items.Clear();

            Nullable<int> EmpType = string.IsNullOrEmpty(Session["EMPTYPE"].ToString()) ? (int?)null : Int32.Parse(Session["EMPTYPE"].ToString());

            DataSet ds = posBLL.LoadPositions((int?)null, EmpType, (int?)null, e.Text);
            DataTable dt = null;
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbOldPosition.Items.Add(new RadComboBoxItem(dt.Rows[i]["POS_TEXT"].ToString(), dt.Rows[i]["POS_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdPosRef = new TableCell();
                Label lblPosRef = new Label();
                lblPosRef.Text = dt.Rows[i]["POS_REF"].ToString();
                tdPosRef.Width = Unit.Pixel(100);
                tdPosRef.Controls.Add(lblPosRef);
                tr.Cells.Add(tdPosRef);

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["POS_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(100);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                //TableCell tdClassSpecName = new TableCell();
                //Label lblClassSpecName = new Label();
                //lblClassSpecName.Text = dt.Rows[i]["CLASS_SPEC_NAME"].ToString();
                //tdClassSpecName.Width = Unit.Pixel(100);
                //tdClassSpecName.Controls.Add(lblClassSpecName);
                //tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(150);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbOldPosition.Items[j].FindControl("tableOldPosition")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbOldClass_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbOldClass.Items.Clear();

            Nullable<int> EmpType = string.IsNullOrEmpty(Session["EMPTYPE"].ToString()) ? (int?)null : Int32.Parse(Session["EMPTYPE"].ToString());
            Nullable<int> PosSequence = string.IsNullOrEmpty(cmbOldPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbOldPosition.SelectedValue);

            DataSet ds = null;
            DataTable dt = null;

            ds = cBLL.LoadClassList((int?)null, EmpType, (int?)null, PosSequence, (int?)null);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            //int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbOldClass.Items.Add(new RadComboBoxItem(dt.Rows[i]["CLASS_NAME"].ToString(), dt.Rows[i]["CLASS_SEQ"].ToString()));
                //j++;
            }
        }
        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbEmployee.Items.Clear();

            DataSet ds = empreqBLL.LoadEmployeeRequest((int?)null, (int?)null, "", "", e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdPosRef = new TableCell();
                Label lblPosRef = new Label();
                lblPosRef.Text = dt.Rows[i]["FULL_NAME"].ToString();
                tdPosRef.Width = Unit.Pixel(150);
                tdPosRef.Controls.Add(lblPosRef);
                tr.Cells.Add(tdPosRef);

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["POS_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(100);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(150);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                TableCell tdSalary = new TableCell();
                Label lblSalary = new Label();
                lblSalary.Text = dt.Rows[i]["SALARY"].ToString();
                tdSalary.Width = Unit.Pixel(50);
                tdSalary.Controls.Add(lblSalary);
                tr.Cells.Add(tdSalary);


                ((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbEmployee_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void cmbOldJobSite_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbOldJobSite.Items.Clear();

            DataSet ds = jsBLL.LoadJobSiteSearchText(SystemInfo._JobsiteExternal, "", e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbOldJobSite.Items.Add(new RadComboBoxItem(dt.Rows[i]["JOB_SITE_NAME"].ToString(), dt.Rows[i]["JOB_SITE_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdFieldName = new TableCell();
                Label lblFieldName = new Label();
                lblFieldName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
                tdFieldName.Width = Unit.Pixel(300);
                tdFieldName.Controls.Add(lblFieldName);
                tr.Cells.Add(tdFieldName);

                ((Table)cmbOldJobSite.Items[j].FindControl("tableNewJobSite")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }        

    }
}